/**
 * @file    OZ7717.h
 * @brief   设备驱动-AFE-OZ7717
 * @author  liuwei
 * @version 1.0.1
 * @date    2023-04-26
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-26 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

#ifndef __OZ7717_H_
#define __OZ7717_H_

/* file includes -------------------------------------------------------------*/
#include <stdint.h>
#include "drv_mcu.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cfg_prj.h"
/* cplusplus -----------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/* macro/define/typedef ------------------------------------------------------*/
typedef union 
{
	uint16_t alarm;
	struct
	{
		uint16_t cov:1;
		uint16_t cuv:1;
		uint16_t occ:1;
		uint16_t ocd:1;
        uint16_t ocd2:1;
		uint16_t otc:1;
		uint16_t utc:1;
		uint16_t otd:1;
		uint16_t utd:1;
		uint16_t sc:1;
        uint16_t other;
	}bit;
}afe_alm_type;

# if (SYS_SC_CURR == 1)
#define OZ7717_DC2_CURR              (80)  // mV 100a
#define OZ7717_SC_CURR               (OZ7717_DOC2SC_SCP_TH_4X_DOC2)  /*!<短路保护电压 4倍*mV*/
#define OZ7717_SC_DELAY              (150) /*!<短路保护延时 uS*/
#endif
# if (SYS_SC_CURR == 2)
#define OZ7717_DC2_CURR              (40) // mV 150A 
#define OZ7717_SC_CURR               (OZ7717_DOC2SC_SCP_TH_4X_DOC2)  /*!<短路保护电压 2倍*mV*/
#define OZ7717_SC_DELAY              (200) /*!<短路保护延时 uS*/
#endif
# if (SYS_SC_CURR == 3)
#define OZ7717_DC2_CURR              (40) // mV 8S200A 
#define OZ7717_SC_CURR               (OZ7717_DOC2SC_SCP_TH_2X_DOC2)  /*!<短路保护电压 2倍*mV*/
#define OZ7717_SC_DELAY              (100) /*!<短路保护延时 uS*/
#endif
# if (SYS_SC_CURR == 4)
#define OZ7717_DC2_CURR              (50) // mV 8S200A 
#define OZ7717_SC_CURR               (OZ7717_DOC2SC_SCP_TH_2X_DOC2)  /*!<短路保护电压 2倍*mV*/
#define OZ7717_SC_DELAY              (150) /*!<短路保护延时 uS*/
#endif

#define OZ7717_COV_VOLT              (4350) /*!<单体过压保护值 */
#define OZ7717_COV_DELAY             (2000) /*!<单体过压保护延时 */
#define OZ7717_OCC_VOLT              (200) /*!<充电过流1保护值*/
#define OZ7717_OCC_DELAY             (200) /*!<充电过流1保护延时*/
#define OZ7717_OCD_VOLT              (200) /*!<放电过流2保护值*/
#define OZ7717_OCD_DELAY             (200) /*!<放电过流1保护延时*/
#define OZ7717_OCD2_VOLT             (200) /*!<放电过流2保护电压 */
#define OZ7717_OCD2_DELAY            (200) /*!<放电过流2保护延时*/

#define OZ7717_CELL_NUM              (17)

#define oz7717_delay_ms              vTaskDelay
//#define oz7717_delay_ms              mcu_delay_ms
/* variables -----------------------------------------------------------------*/

/*******************************************************************************
 * public API List
 ******************************************************************************/
uint8_t oz7717_init(void);
uint8_t oz7717_set_power_mode(uint16_t pmode);
uint8_t oz7717_ctrl_mos(uint8_t cmos , uint8_t dmos);
uint8_t oz7717_read_mos_state(uint8_t *cmos , uint8_t *dmos);
uint8_t oz7717_read_alarm_state(afe_alm_type *alarm_msg);
uint8_t oz7717_read_comm_state(void);
uint8_t oz7717_read_clear_alarm(void);
uint8_t oz7717_read_clear_sc(void);
uint8_t oz7717_read_volt(uint8_t cell_num , uint16_t *volt_buf);
uint8_t oz7717_read_packc_volt(double *curr);
uint8_t oz7717_read_coulometer_volt(double *curr);
uint8_t oz7717_config_init(void);
uint8_t oz7717_balance_ctrl(uint32_t set_val,uint8_t cell_num);
uint8_t oz7717_read_reg(uint8_t reg_addr , uint16_t *rd_data);
uint8_t oz7717_write_reg(uint8_t reg_addr , uint16_t data);
void oz7717_wake_up(void);
void oz7717_into_sleep(void);

#ifdef __cplusplus
}
#endif

#endif  /* #ifndef __OZ7717_H_ */
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
